<script setup lang="ts" name="User">
import editPage from "./edit.vue";
import detailPage from "./detail.vue";

const { proxy } = getCurrentInstance();

// const { sys_user_sex, sys_show_hide, sys_user_interest } = useDict("sys_user_sex", "sys_show_hide", "sys_user_interest");

// 加载
const loading = ref(false);

// 导出遮罩层
const exportLoading = ref(false);

// 搜索栏元素
const queryUserRef = ref(null);

// 表单Id
const id = ref(null);

// 选中数组
const selection = ref<UserRecord[]>([]);

// 非单个禁用
const single = ref(true);

// 非多个禁用
const multiple = ref(true);

// 切换搜索条件
const showSearch = ref(false);

// 用户列表
const userList = ref<UserRecord[]>([]);

// 弹出层标题
const title = ref("");

// 编辑弹出层
const openEdit = ref(false);

// 详情弹出层
const openDetail = ref(false);

//总条数
const total = ref(0);

// 列信息
const columns = ref<TableColumnsRecord>({
  colNo: { label: "序号", visible: true },
  userId: { label: "ID", visible: true },
  userName: { label: "名字", visible: true },
  userAge: { label: "年龄", visible: true },
  userSex: { label: "性别", visible: true },
  userImg: { label: "头像", visible: true },
  userInterest: { label: "爱好", visible: true },
  userBirthdayTime: { label: "生日", visible: true },
  userDescribe: { label: "描述", visible: true },
  operate: { label: "操作", visible: true }
});

// 查询参数
const queryParams = ref<UserRecord>({
  pageNum: 1,
  pageSize: 10,
  userId: null,
  userName: null,
  userAge: null,
  userSex: null,
  userImg: null,
  userInterest: null,
  userBirthdayTime: null,
  userDescribe: null,
});

/** 查询用户列表 */
function getList() {
  loading.value = true;
  listUserApi(queryParams.value).then(response => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    userList.value.forEach((item, index) => {
      item['colNo'] = sequenceNo(index, queryParams.value);
    });
  });
}

/** 关闭弹窗 */
function closeDialog(refresh: boolean) {
  if (refresh) {
    getList();
  }
  openEdit.value = false;
  openDetail.value = false;
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryUserRef.value.resetFields();
  handleQuery();
}

/** 切换按钮操作 */
function changSearch() {
  showSearch.value = !showSearch.value;
}

/** 多选框选中数据 */
function handleSelectionChange(selectionList: UserRecord[]) {
  selection.value = selectionList;
  single.value = selectionList.length !== 1;
  multiple.value = !selectionList.length;
}

/** 新增按钮操作 */
function handleAdd() {
  id.value = null;
  openEdit.value = true;
  title.value = "新增用户";
}

/** 修改按钮操作 */
function handleUpdate(row: UserRecord) {
  id.value = row.userId;
  openEdit.value = true;
  title.value = "修改用户";
}

/** 详情按钮操作 */
function handleDetails(row: UserRecord) {
  id.value = row.userId;
  openDetail.value = true;
  title.value = "用户详情";
}

/** 删除按钮操作 */
function handleDelete(row: UserRecord | any) {
  let ids = [];
  let colNos = [];
  if (row.userId) {
    ids = [row.userId];
    colNos = [row.colNo];
  } else {
    selection.value.forEach(item => {
      ids.push(item.userId);
      colNos.push(item.colNo);
    });
  }

  // @ts-ignore
  proxy.$modal.confirm('是否确认删除序号为"' + colNos + '"的数据项?').then(() => {
    return delUserApi(ids);
  }).then(() => {
    getList();
    // @ts-ignore
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  download('system/user/export', {
    ...queryParams.value
  }, `用户_${new Date().getTime()}.xlsx`);
}

getList();
</script>

<template>
  <div class="app-container">
    <div class="back-item">
      <el-form :model="queryParams" ref="queryUserRef" :inline="true" label-width="68px">
        <el-form-item label="ID" prop="userId" v-if="showSearch">
          <el-input
              v-model="queryParams.userId"
              placeholder="请输入ID"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="名字" prop="userName" v-if="showSearch">
          <el-input
              v-model="queryParams.userName"
              placeholder="请输入名字"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="年龄" prop="userAge" v-if="showSearch">
          <el-input
              v-model="queryParams.userAge"
              placeholder="请输入年龄"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="性别" prop="userSex" v-if="showSearch">

        </el-form-item>
        <el-form-item label="头像" prop="userImg" v-if="showSearch">

        </el-form-item>
        <el-form-item label="爱好" prop="userInterest" v-if="showSearch">
        </el-form-item>
        <el-form-item label="生日" prop="userBirthdayTime" v-if="showSearch">
          <el-date-picker
              v-model="queryParams.userBirthdayTime"
              type="datetime"
              placeholder="请选择生日"
              value-format="YYYY-MM-DD HH:mm:ss"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="描述" prop="userDescribe" v-if="showSearch">
          <el-input
              v-model="queryParams.userDescribe"
              placeholder="请输入描述"
              clearable
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item class="item-search">
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button type="warning" icon="Refresh" @click="resetQuery">重置</el-button>
          <el-button
              :icon="showSearch ? 'CaretTop' : 'CaretBottom'"
              @click="changSearch"
          >
            {{ showSearch ? "折叠" : "更多" }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="back-item">
      <div class="table-top mb8">
        <div class="title title-left">
          用户列表
        </div>
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-has-permi="['system:user:add']"
        >新增
        </el-button>
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-has-permi="['system:user:remove']"
        >删除
        </el-button>
        <el-button
            type="warning"
            plain
            icon="Download"
            :loading="exportLoading"
            @click="handleExport"
            v-has-permi="['system:user:export']"
        >导出
        </el-button>
        <span class="table-top-line" />
        <right-toolbar @queryTable="getList" :columns="columns" />
      </div>
      <el-table
          v-loading="loading"
          :data="userList"
          stripe
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" prop="colNo" align="center" width="55" v-if="columns.colNo.visible" />
        <el-table-column label="ID" prop="userId" align="center" v-if="columns.userId.visible" />
        <el-table-column label="名字" prop="userName" align="center" v-if="columns.userName.visible" />
        <el-table-column label="年龄" prop="userAge" align="center" v-if="columns.userAge.visible" />
        <el-table-column label="性别" prop="userSex" align="center" v-if="columns.userSex.visible">
        </el-table-column>
        <el-table-column label="头像" prop="userImg" align="center" v-if="columns.userImg.visible">

        </el-table-column>
        <el-table-column label="爱好" prop="userInterest" align="center" v-if="columns.userInterest.visible">

        </el-table-column>
        <el-table-column label="生日" prop="userBirthdayTime" align="center" v-if="columns.userBirthdayTime.visible" />
        <el-table-column label="描述" prop="userDescribe" align="center" v-if="columns.userDescribe.visible" />
        <el-table-column label="操作" prop="operate" align="center" fixed="right" width="120" v-if="columns.operate.visible">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Edit"
                  @click="handleUpdate(scope.row)"
                  v-has-permi="['system:user:edit']"
              />
            </el-tooltip>
            <el-tooltip content="详情" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Document"
                  @click="handleDetails(scope.row)"
                  v-has-permi="['system:user:query']"
              />
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                  link
                  type="primary"
                  icon="Delete"
                  @click="handleDelete(scope.row)"
                  class="text-danger"
                  v-has-permi="['system:user:remove']"
              />
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination
          :total="total"
          open-fixed
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
      <div class="line-magin" />
    </div>

    <!-- 添加或修改用户对话框 -->
<!--    <edit-page v-if="openEdit" :id="id" :title="title" @closeDialog="closeDialog" />-->
<!--    <detail-page v-if="openDetail" :id="id" :title="title" @closeDialog="closeDialog" />-->
  </div>
</template>

<style lang="scss" scoped>

</style>